﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using AMM.Model;
using AMM.Model.Finance;

namespace AMM.DataAccess.Finance {
    public class BankAccountRepository : BaseRepository<BankAccount> {

        private int _pageSize;

        public BankAccountRepository(int pageSize) {
            this._pageSize = pageSize;
        }

        public BankAccount FetchBankAccountById(int bankAccountId) {
            using (var ctx = this.Context) {
                return ctx.BankAccounts.Find(bankAccountId);
            }
        }

        public void DeleteMultipleBankAccount(params int[] bankAccountIds) {
            using (var ctx = this.Context) {
                using (DbContextTransaction trans = ctx.Database.BeginTransaction()) {
                    try {
                        ctx.BankAccounts.Where(x => bankAccountIds.Contains(x.BankAccountId)).ToList().ForEach(x => {
                            ctx.BankAccounts.Remove(x);
                        });
                        ctx.SaveChanges();
                        trans.Commit();
                    }
                    catch (Exception ex) {
                        trans.Rollback();
                        throw ex;
                    }
                }
            }
        }

        public List<BankAccount> FetchBankAccountList() {
            using (var ctx = this.Context) {
                return ctx.BankAccounts.ToList();
            }
        }

       public List<BankAccount> FetchBankAccountList(BankAccountSearchModel searchModel, int pageIndex) {
            using (var ctx = this.Context) {
                var query = ctx.BankAccounts
                    .Include("Basic")
                    .Include("Basic1")
                    .Where(x => !string.IsNullOrEmpty(x.Code));

                if (!string.IsNullOrEmpty(searchModel.Code)) {
                    query = query.Where(x => x.Code.Contains(searchModel.Code));
                }
                if (!string.IsNullOrEmpty(searchModel.BankName)) {
                    query = query.Where(x => x.BankName.Contains(searchModel.BankName));
                }
                if (!string.IsNullOrEmpty(searchModel.BankAccountName)) {
                    query = query.Where(x => x.BankAccountName.Contains(searchModel.BankAccountName));
                }
                if (searchModel.CurrencyId.HasValue) {
                    query = query.Where(x => x.CurrencyId == searchModel.CurrencyId.Value);
                }
                if (searchModel.FinancialAccountTypeId.HasValue) {
                    query = query.Where(x => x.FinancialAccountTypeId == searchModel.FinancialAccountTypeId.Value);
                }
                if (searchModel.IsEnabled.HasValue) {
                    query = query.Where(x => x.IsEnabled == searchModel.IsEnabled.Value);
                }

                searchModel.Records = query.Count();
                pageIndex = (pageIndex - 1) * this._pageSize > searchModel.Records ? 1 : pageIndex;

                return query.OrderBy(x => x.BankAccountId).Skip((pageIndex - 1) * this._pageSize).Take(this._pageSize).ToList();
            }
        }
       
    }
}
